package com.webvolcano.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @author evyatar
 *
 */
public class FileUtils {

	public static void concat(List<File> files, OutputStream out)
			throws IOException {
		File[] filesArray = new File[files.size()];
		int i = 0;
		for (File file : files) {
			filesArray[i++] = file;
		}
		FileUtils.concat(filesArray, out);
	}

	public static void concat(String folder, OutputStream out)
			throws IOException {
		File[] filesArray = new File(folder).listFiles();
		FileUtils.concat(filesArray, out);
	}

	public static void concat(File[] files, OutputStream out)
			throws IOException {
		byte[] bytes = new byte[1024];
		for (File file : files) {
			FileInputStream fis = new FileInputStream(file);
			int len = -1;
			while ((len = fis.read(bytes)) != -1) {
				out.write(bytes, 0, len);
			}
			fis.close();
		}
		out.close();
	}
	
	public static void main(String[] args) throws Exception{
		FileOutputStream fos = new FileOutputStream(new File ("C:\\temp\\s3.al.txt"));
		String folder = "C:\\temp\\al";
		FileUtils.concat(folder, fos);		
	}
}
